//
//  TabBarController.swift
//  DHLive
//
//  Created by Mac on 2021/6/27.
//

import UIKit

class TabBarController: UITabBarController {

    lazy var tabbar: TabBar = {
        let tb = TabBar(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kHeight_TabBar))
        tb.tbDelegate = self
        return tb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configTabBar()
        setupVCs()
    }
}

extension TabBarController {
    func setupVCs() {
        var views = [UIViewController]()
        let vcNames = ["ShowViewController", "MineViewController"]
        for name in vcNames {
            guard let nameSpace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String else {
                print("没有命名空间")
                return
            }
            
            guard let className = NSClassFromString(nameSpace + "." + name) else {
                print("没有对应的类名")
                return
            }
            
            guard let classVC = className as? UIViewController.Type else {
                print("没有转成对应的控制器类型")
                return
            }
            let childController = classVC.init()
            let navi = BaseNaviViewController(rootViewController: childController)
            views.append(navi)
        }
        viewControllers = views
    }
    
    func configTabBar() {
        setValue(tabbar, forKey: "tabBar")
    }
}


extension TabBarController: TabBarDelegate {
    func tabbarDelegate(_ clickButton: Int) {
        if clickButton == ItemType.ItemTypeLaunch.rawValue {
            let launchVC = LaunchViewController()
            launchVC.modalPresentationStyle = .fullScreen
            present(launchVC, animated: true, completion: nil)
        }else {
            selectedIndex = clickButton - ItemType.ItemTypeShow.rawValue
        }
        
    }
}
